require 'thread'
require 'drb'
require 'drb/observer'
require 'logger'
require 'singleton'
require 'digest/md5'
require 'yaml'
require 'monitor'

Thread.abort_on_exception=true

$ACTIVE_BACKGROUND_DEBUG = true

# logger
require 'logger'
ACTIVE_BACKGROUND_LOGGER = Logger.new(File.dirname(__FILE__) + '/log/active_background.log')
ACTIVE_BACKGROUND_LOGGER.level = Logger::DEBUG
ACTIVE_BACKGROUND_LOGGER.datetime_format = "%H:%M:%S"

# base class
require File.dirname(__FILE__) + '/lib/active_background.rb'

# setup our client/server
require File.dirname(__FILE__) + '/lib/active_background/server.rb'
require File.dirname(__FILE__) + '/lib/active_background/client.rb'

# we needs some people to help us out with the jobs
require File.dirname(__FILE__) + '/lib/active_background/manager.rb'
require File.dirname(__FILE__) + '/lib/active_background/assistent.rb'

# archive processed and failed jobs
require File.dirname(__FILE__) + '/lib/active_background/archive.rb'
require File.dirname(__FILE__) + '/lib/active_background/archive_handlers/database.rb'
require File.dirname(__FILE__) + '/lib/active_background/archive_handlers/file.rb'
require File.dirname(__FILE__) + '/lib/active_background/archive_handlers/yaml.rb'

# some helper classes to make configuration more efficient
require File.dirname(__FILE__) + '/lib/active_background/configuration.rb'
require File.dirname(__FILE__) + '/lib/active_background/access_control.rb'

# database
require File.dirname(__FILE__) + '/lib/active_background/database.rb'

# TODO: need to wrap this in the database method of worker...
require 'active_record'
require File.dirname(__FILE__) + "/../../../config/boot.rb"
ActiveRecord::Base.allow_concurrency = true
ActiveRecord::Base.establish_connection(YAML.load(ERB.new(IO.read("#{RAILS_ROOT}/config/database.yml")).result)['development'])


# worker management
require File.dirname(__FILE__) + '/lib/active_background/job.rb'
require File.dirname(__FILE__) + '/lib/active_background/jobs/worker.rb'
require File.dirname(__FILE__) + '/lib/active_background/jobs/queue.rb'

# a way to communicate with the server without being consired
# of all the connection, configuration and job management details.
require File.dirname(__FILE__) + '/lib/active_background/middle_man.rb'

MiddleMan = ActiveBackground::MiddleMan.instance

require File.dirname(__FILE__) + '/lib/active_background/handler.rb'

require File.dirname(__FILE__) + '/lib/active_background/handlers/chef.rb'
MiddleMan.register(ActiveBackground::Chef.new)

require File.dirname(__FILE__) + '/lib/active_background/handlers/cache.rb'
MiddleMan.register(ActiveBackground::Cache.new)